package com.pbase.data.DataStore;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.Utils;

import java.lang.reflect.Type;

import androidx.datastore.preferences.core.MutablePreferences;
import androidx.datastore.preferences.core.Preferences;
import androidx.datastore.preferences.rxjava3.RxPreferenceDataStoreBuilder;
import androidx.datastore.rxjava3.RxDataStore;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Single;

/**
 * User: psn
 * Date: 2021/7/19
 * Preferences DataStore 代替sp文件
 * 将文件名与键名定义在DataStore_key文件中，并在application中调用DataStore_key的初始化方法为DATASTORE_NAEM赋值
 */
public class DataStore_Tool {
    //文件名
    public static String DATASTORE_NAEM;


    private static DataStore_Tool INSTANCE;
    //数据存储对象
    private RxDataStore<Preferences> mDataStore;

    private DataStore_Tool() {
        mDataStore = new RxPreferenceDataStoreBuilder(Utils.getApp().getApplicationContext(),
                DATASTORE_NAEM).build();

    }

    public static DataStore_Tool getInstance() {
        if (INSTANCE == null) {
            synchronized (DataStore_Tool.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DataStore_Tool();
                }
            }
        }
        return INSTANCE;
    }

    public void setDataValue(Preferences.Key<String> key, String value) {
        setPreferencesDataValue(key, value);
    }

    public void setDataValue(Preferences.Key<Integer> key, int value) {
        setPreferencesDataValue(key, value);
    }

    public void setDataValue(Preferences.Key<Long> key, long value) {
        setPreferencesDataValue(key, value);
    }

    public void setDataValue(Preferences.Key<Boolean> key, boolean value) {
        setPreferencesDataValue(key, value);
    }

    public void setDataValue(Preferences.Key<Float> key, float value) {
        setPreferencesDataValue(key, value);
    }

    public void setDataValue(Preferences.Key<Double> key, double value) {
        setPreferencesDataValue(key, value);
    }

    private void setPreferencesDataValue(Preferences.Key key, Object value) {
        Single<Preferences> updateResult = mDataStore.updateDataAsync(preferences -> {
            MutablePreferences mutablePreferences = preferences.toMutablePreferences();
            mutablePreferences.set(key, value);
            return Single.just(mutablePreferences);
        });
    }

    public <T> T getDataValue(Preferences.Key<String> key, String defaultValue, Class<T> classOfT) {
        T t = null;
        String value = getDataValue(key, defaultValue);
        if (value != null) {
            t = GsonUtils.fromJson(value, classOfT);
        }
        return t;
    }
    public <T> T getDataValue(Preferences.Key<String> key, String defaultValue, Type type) {
        T t = null;
        String value = getDataValue(key, defaultValue);
        if (value != null) {
            t = GsonUtils.fromJson(value, type);
        }
        return t;
    }
    public String getDataValue(Preferences.Key<String> key, String defaultValue) {
        Object value = getPreferencesDataValue(key);
        return (String) (value != null ? value : defaultValue);
    }

    public int getDataValue(Preferences.Key<Integer> key, int defaultValue) {
        Object value = getPreferencesDataValue(key);
        return (int) (value != null ? value : defaultValue);
    }

    public long getDataValue(Preferences.Key<Long> key, long defaultValue) {
        Object value = getPreferencesDataValue(key);
        return (long) (value != null ? value : defaultValue);
    }

    public boolean getDataValue(Preferences.Key<Boolean> key, boolean defaultValue) {
        Object value = getPreferencesDataValue(key);
        return (boolean) (value != null ? value : defaultValue);
    }

    public float getDataValue(Preferences.Key<Float> key, float defaultValue) {
        Object value = getPreferencesDataValue(key);
        return (float) (value != null ? value : defaultValue);
    }

    public double getDataValue(Preferences.Key<Double> key, double defaultValue) {
        Object value = getPreferencesDataValue(key);
        return (double) (value != null ? value : defaultValue);
    }

    private Object getPreferencesDataValue(Preferences.Key key) {
        Flowable<Object> carIdFlow = mDataStore.data().map(preferences -> preferences.get(key));
        try {
            //同步等待返回值
            return carIdFlow.blockingFirst();
        } catch (Exception e) {
            e.fillInStackTrace();
            //查询不到默认返回null
            return null;
        }
    }

}
